<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="deviceName">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="连接状态" prop="isConnect">
        <el-select v-model="queryParams.isConnect" placeholder="请选择连接状态" clearable>
          <el-option label="在线" value="1" />
          <el-option label="离线" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="isConnect">
        <el-select v-model="queryParams.status" placeholder="请选择设备状态" clearable>
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['game:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['game:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['game:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['game:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 手机设备列表区域 - 使用背景色区分状态 -->
    <div v-loading="loading" class="device-list">
      <div v-for="device in deviceList" :key="device.id" class="device-card">
        <div
          :class="{
            'disabled': device.status !== '0',
            'offline': device.isConnect === '0' && device.status === '0',
            'online': device.isConnect !== '0' && device.status === '0'
          }"
          class="device-block"
        >
          <!-- 备注信息 - 右上角 -->
          <div v-if="device.remark" class="device-remark">
            <el-tooltip :content="'备注: ' + device.remark" class="item" effect="dark" placement="top">
              <i class="el-icon-info"></i>
            </el-tooltip>
          </div>

          <!-- 手机设备图标 -->
          <div class="device-icon">
            <i class="el-icon-mobile-phone"></i>
          </div>

          <!-- 设备名称 -->
          <div :title="device.deviceName" class="device-name">
            {{ device.deviceName }}
          </div>

          <!-- 操作按钮 -->
          <div class="device-actions">
            <el-button
              size="mini"
              type="text"
              circle
              icon="el-icon-edit"
              @click="handleUpdate(device)"
              v-hasPermi="['game:device:edit']"
            ></el-button>
            <el-button
              size="mini"
              type="text"
              circle
              icon="el-icon-delete"
              @click="handleDelete(device)"
              v-hasPermi="['game:device:remove']"
            ></el-button>
          </div>

          <!-- 设备ID - 调整宽度和字体 -->
          <div class="device-id" @click="copyDeviceId(device.id)">
            <span>ID: {{ device.id }}</span>
            <i class="el-icon-document-copy"></i>
          </div>
        </div>
      </div>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改游戏设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="deviceName">
          <el-input size="small" v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="'0'"
            :inactive-value="'1'"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input size="small" v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addDevice, delDevice, getDevice, listDevice, updateDevice} from "@/api/game/device";

export default {
  name: "Device",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 游戏设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        deviceName: null,
        gameType: null,
        isConnect: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询游戏设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        deviceName: null,
        gameType: null,
        isConnect: null,
        status: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加游戏设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改游戏设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除游戏设备编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('game/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    },
    /** 复制设备ID */
    copyDeviceId(id) {
      const input = document.createElement('input');
      input.value = id;
      document.body.appendChild(input);
      input.select();
      navigator.clipboard.writeText(input.value);
      document.body.removeChild(input);
      this.$message.success('已复制');
    }
  }
};
</script>

<style scoped>
/* 设备列表容器 */
.device-list {
  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

/* 设备卡片 - 固定尺寸 */
.device-card {
  width: 180px;
  margin-bottom: 16px;
}

/* 设备块 - 固定高度 */
.device-block {
  width: 100%;
  height: 200px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: all 0.3s;
  position: relative;
  overflow: hidden;
}

/* 设备状态样式 - 使用背景色区分 */
.device-block.online {
  background: linear-gradient(to bottom, #fff, #ecf5ff);
  border-left: 3px solid #409EFF;
}

.device-block.offline {
  background: linear-gradient(to bottom, #fff, #f5f5f5);
  border-left: 3px solid #909399;
}

.device-block.disabled {
  background: linear-gradient(to bottom, #fff, #fef0f0);
  border-left: 3px solid #F56C6C;
}

/* 备注信息 - 右上角 */
.device-remark {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
}

.device-remark i {
  color: rgba(58, 58, 58, 0.55);
  cursor: pointer;
  font-size: 16px;
}

/* 手机设备图标 */
.device-icon {
  position: relative;
  margin: 10px 0 15px;
}

.device-icon i {
  font-size: 36px;
}

/* 根据状态调整图标颜色 */
.device-block.online .device-icon i {
  color: #409EFF;
}

.device-block.offline .device-icon i {
  color: #909399;
}

.device-block.disabled .device-icon i {
  color: #F56C6C;
}

/* 设备名称 */
.device-name {
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 15px;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

/* 操作按钮 */
.device-actions {
  display: flex;
  justify-content: center;
  gap: 15px;
  width: 100%;
  margin-top: auto;
  margin-bottom: 10px;
}

.device-actions .el-button--text {
  width: 32px;
  height: 32px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s ease;
  background-color: rgba(0, 0, 0, 0.03);
}

.device-actions .el-button--text:first-child {
  color: #409EFF;
}

.device-actions .el-button--text:first-child:hover {
  background-color: rgba(64, 158, 255, 0.1);
  transform: translateY(-2px);
}

.device-actions .el-button--text:last-child {
  color: #F56C6C;
}

.device-actions .el-button--text:last-child:hover {
  background-color: rgba(245, 108, 108, 0.1);
  transform: translateY(-2px);
}

/* 设备ID - 调整宽度和字体 */
.device-id {
  font-size: 11px; /* 调整为11px */
  color: rgba(58, 58, 58, 0.94);
  text-align: center;
  cursor: pointer;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 5px 10px;
  border-radius: 15px;
  background-color: rgba(0, 0, 0, 0.03);
  transition: all 0.2s;
  width: 100%; /* 调整为100% */
}

.device-id:hover {
  color: #409EFF;
  background-color: rgba(64, 158, 255, 0.1);
}

.device-id i {
  font-size: 11px;
}

</style>
